package com.java.day4carsystem.service;

import com.java.day4carsystem.entity.Bus;
import com.java.day4carsystem.entity.Truck;
import com.java.day4carsystem.entity.Van;
import com.java.day4carsystem.entity.Vehicle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 系统实现类
 */
public class RentSystemImpl implements RentSystem{

    //汽车集合
    private List<Vehicle> vehicles = new ArrayList<>();

    private Scanner scanner = new Scanner(System.in);

    @Override
    public Vehicle createVehicle() {
        //输入汽车信息
        System.out.println("请输入汽车的牌照");
        String number = scanner.next();
        //车牌号不能重复
        boolean exists = vehicles.stream().filter((c) -> c.getNumber().equals(number)).findFirst().isPresent();
        if(exists){
            System.out.println("该车牌已经存在");
            return null;
        }
        System.out.println("请输入汽车的型号");
        String type = scanner.next();
        System.out.println("请输入汽车的价格");
        float price = scanner.nextFloat();
        System.out.println("请输入汽车的颜色");
        String color = scanner.next();
        System.out.println("请输入汽车的排量");
        float gasValue = scanner.nextFloat();
        System.out.println("请输入汽车的类型:1、巴士 2、货车 3、卡车");
        int carType = scanner.nextInt();
        Vehicle vehicle = null;
        switch(carType){
            case 1:
                System.out.println("请输入汽车的座位数：");
                int seats = scanner.nextInt();
                vehicle = new Bus(number,type,price,color,gasValue,seats);
                break;
            case 2:
                System.out.println("请输入汽车的车厢大小：");
                int size = scanner.nextInt();
                vehicle = new Van(number,type,price,color,gasValue,size);
                break;
            case 3:
                System.out.println("请输入汽车的载重：");
                int load = scanner.nextInt();
                vehicle = new Truck(number,type,price,color,gasValue,load);
                break;
            default:
                System.out.println("类型错误");
        }
        return vehicle;
    }

    @Override
    public void printVehicles() {
        System.out.println("请选择排序方式：1、按价格排序 2、按车牌号排序 3、按排量排序");
        int num = scanner.nextInt();
        switch (num){
            case 1:
                Collections.sort(vehicles,(c1,c2) -> {
                    if(c1.getPrice() > c2.getPrice())
                        return 1;
                    else if(c1.getPrice() < c2.getPrice())
                        return -1;
                    else
                        return 0;
                });
                break;
            case 2:
                Collections.sort(vehicles,(c1,c2) -> c1.getNumber().compareTo(c2.getNumber()));
                break;
            case 3:
                Collections.sort(vehicles,(c1,c2) -> {
                    if(c1.getGasValue() > c2.getGasValue())
                        return 1;
                    else if(c1.getGasValue() < c2.getGasValue())
                        return -1;
                    else
                        return 0;
                });
                break;
        }
        vehicles.forEach((v) -> v.print());
    }

    @Override
    public void deleteVehicle(String number) {

    }

    @Override
    public boolean rentVehicle(String number) {
        return false;
    }

    @Override
    public boolean returnVehicle(String number) {
        return false;
    }

    @Override
    public void startSystem() {
        System.out.println("欢迎使用汽车租赁系统");
        for(;;){
            System.out.println("请选择操作：1、添加汽车 2、显示汽车 3、删除汽车 4、租赁汽车 5、归还汽车 0、退出");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    Vehicle vehicle = createVehicle();
                    if(vehicle != null){
                        vehicles.add(vehicle);
                        System.out.println("添加成功");
                    }else{
                        System.out.println("添加失败");
                    }
                    break;
                case 2:
                    printVehicles();
                    break;
                default:
                    return;
            }
        }
    }

    public static void main(String[] args) {
        //创建系统
        RentSystem rentSystem = new RentSystemImpl();
        rentSystem.startSystem();
    }
}
